using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmClass : Form
    {
        List<Entities.Class> classList = new List<Entities.Class>();
        List<Entities.Course> courseList = new List<Entities.Course>();
        List<Entities.Person> personList = new List<Entities.Person>();
        List<Entities.Student> studentList = new List<Entities.Student>();
        Business.ClassService classService = new Business.ClassService();
        Business.CourseService courseService = new Business.CourseService();
        Business.PersonService personService = new Business.PersonService();
        Business.StudentService StudentService = new Business.StudentService();
        Business.LoginService loginService = new Business.LoginService();
        Entities.Class _class = null;
        int iIndex = 0;

        public FrmClass()
        {
            InitializeComponent();
        }

        private void InitializeClass()
        {
            if (dgvClass.Rows.Count > 1)
            {
                int iCount = dgvClass.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvClass.Rows.RemoveAt(0);
                }
            }
            classList = classService.GetAllClasses();
            foreach (Entities.Class _class in classList)
            {
                string[] temp = { _class.ClassID, _class.ClassName, _class.CourseID};
                dgvClass.Rows.Add(temp);
            }
            dgvClass[0, iIndex].Selected = true;
        }
        
        private void InitializeCourse()
        {
            courseList = courseService.GetAllCourses();
            foreach (Entities.Course course in courseList)
            {
                cmbCourse.Items.Add(course.CourseID +"--"+course.CourseName);
            }
        }

        private void FindClassByClassID(string sClassID)
        {
            classList = classService.GetAllClasses();
            if (dgvClass.Rows.Count > 1)
            {
                int iCount = dgvClass.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvClass.Rows.RemoveAt(0);
                }
            }
            foreach (Entities.Class _class in classList)
            {
                if (_class.ClassID.ToLower().Contains(sClassID))
                {
                    string[] temp = { _class.ClassID, _class.ClassName, _class.CourseID };
                    dgvClass.Rows.Add(temp);
                }
            }
            iIndex = 0;
            dgvClass[0, iIndex].Selected = true;
            if (dgvClass.Rows.Count > 1 ) FindClass(dgvClass.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void FindClass(string sClassID)
        {
            _class = new Entities.Class();
            _class = classService.GetClassByClassID(sClassID);
            txtClassID.Text = _class.ClassID;
            txtClassName.Text = _class.ClassName;
            cmbCourse.Text = _class.CourseID;
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (txtClassName.Text == string.Empty)
                sInformation += "ClassName is empty" + "\n";
            if (cmbCourse.Text == string.Empty)
                sInformation += "CourseID is empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void InsertClass()
        {
            if(Test())
            {
                Entities.Class _class = new Entities.Class();
                _class.ClassName = txtClassName.Text;
                _class.CourseID = (cmbCourse.Text.Length > 5 ? cmbCourse.Text.Substring(0, 5) : cmbCourse.Text);
                try
                {
                    classService.InsertClass(_class);
                    iIndex = dgvClass.Rows.Count - 1;
                    InitializeClass();
                }
                catch
                {
                    MessageBox.Show("CourseID you choose isn't legal");
                }
            }
        }

        private void UpdateClass()
        {
            if (Test())
            {
                _class.ClassName = txtClassName.Text;
                _class.CourseID = (cmbCourse.Text.Length > 5 ? cmbCourse.Text.Substring(0, 5) : cmbCourse.Text);
                try
                {
                    classService.UpdateClass(_class);
                    InitializeClass();
                }
                catch
                {
                    MessageBox.Show("CourseID you choose isn't legal");
                }
            }
        }

        private void DeleteClass()
        {
            if (dgvClass.CurrentRow.Index >= 0)
            {
                try
                {
                    classService.DeleteClass(_class.ClassID);
                    if (iIndex > 0) iIndex--;
                    else iIndex = 0;
                    InitializeClass();
                    FindClass(dgvClass.Rows[iIndex].Cells[0].Value.ToString());
                }
                catch
                {
                    MessageBox.Show("You can't delete this class because it have references to some students", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }    
        }

        private void FrmClass_Load(object sender, EventArgs e)
        {
            InitializeClass();
            InitializeCourse();
        }

        private void dgvClass_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvClass.CurrentRow.Index < dgvClass.Rows.Count - 1)
            {
                iIndex = dgvClass.CurrentRow.Index;
                string sClassID = dgvClass.CurrentRow.Cells[0].Value.ToString();
                if (rdoUpdate.Checked)
                {
                    FindClass(sClassID);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (_class != null)
            {
                if (MessageBox.Show("Do you really want to delete this file?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    DeleteClass();
                }
            }
            else
                MessageBox.Show("You should choose the class which you want to delete!");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FindClassByClassID(txtClassID.Text.ToLower());
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            InsertClass();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_class != null)
                UpdateClass();
            else
                MessageBox.Show("You should choose the class which you want to change!");
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            txtClassID.Enabled = true;
            cmbCourse.Text = string.Empty;
            txtClassID.Text = string.Empty;
            txtClassName.Text = string.Empty;
            pictureBox1.Enabled = true;
            btnInsert.Enabled = false;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            FindClass(dgvClass.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void rdoInsert_CheckedChanged(object sender, EventArgs e)
        {
            txtClassID.Enabled = false;
            cmbCourse.Text = string.Empty;
            txtClassID.Text = string.Empty;
            txtClassName.Text = string.Empty;
            pictureBox1.Enabled = false;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }
    }
}